/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.rmi.Naming;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import server.Server_Client_Interface;

/**
 *
 * @author 김기민
 */
public class CustomerUI extends javax.swing.JFrame {

    /**
     * Creates new form ClientUi
     */
    int count = 0;
    private String userid;

    private CustomerUI() {
         initComponents();
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public CustomerUI(String id) {

        initComponents();
        setUserid(id);

    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        lbl_Welcome = new javax.swing.JLabel();
        btb_myinformation = new javax.swing.JButton();
        btb_facebook = new javax.swing.JButton();
        btb_insurunceInformation = new javax.swing.JButton();
        btb_InsurunceJoin = new javax.swing.JButton();
        list1 = new java.awt.List();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jScrollPane2.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbl_Welcome.setFont(new java.awt.Font("굴림", 0, 14)); // NOI18N
        lbl_Welcome.setText(getUserid() + ", Welcome to insurunce Client page");

        btb_myinformation.setText("My information");
        btb_myinformation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btb_myinformationActionPerformed(evt);
            }
        });

        btb_facebook.setText("Post on the facebook");
        btb_facebook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btb_facebookActionPerformed(evt);
            }
        });

        btb_insurunceInformation.setText("Insurunce information");
        btb_insurunceInformation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btb_insurunceInformationActionPerformed(evt);
            }
        });

        btb_InsurunceJoin.setText(" Insurunce Join");
        btb_InsurunceJoin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btb_InsurunceJoinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btb_insurunceInformation, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(btb_myinformation, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(16, 16, 16))
                                .addComponent(btb_facebook, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btb_InsurunceJoin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(135, Short.MAX_VALUE)
                        .addComponent(lbl_Welcome, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(128, 128, 128))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(20, Short.MAX_VALUE)
                                .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(216, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(lbl_Welcome, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btb_myinformation, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btb_facebook, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(btb_insurunceInformation, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btb_InsurunceJoin, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(51, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap(61, Short.MAX_VALUE)
                                .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(26, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>                        

    private void btb_myinformationActionPerformed(java.awt.event.ActionEvent evt) {

        list1.clear();
        ArrayList<String> result = new ArrayList<String>();

        try {
            Server_Client_Interface rmi = (Server_Client_Interface) Naming.lookup("rmi://localhost/MyRMIserver");

            result = rmi.toString_private_insurunce(null);
          
            
           
           if (count == 0) {
                list1.add("Type : " + result.get(0));
                list1.add("Duration : " + result.get(1));
                list1.add("fee : " + result.get(2));
                list1.add("Discount rate depends on age" + result.get(3));
                list1.add("your Insurunce fee is " + result.get(2));
            } else if (count == 1) {
                list1.add("Type : " + result.get(0));
                list1.add("Duration : " + result.get(1));
                list1.add("fee : " + result.get(2));
                list1.add("Discount rate depends on age" + result.get(3));
                list1.add("your Insurunce fee is " + Integer.parseInt(result.get(2)) * 0.95);
            } else if (count == 2) {
                list1.add("Type : " + result.get(0));
                list1.add("Duration : " + result.get(1));
                list1.add("fee : " + result.get(2));
                list1.add("Discount rate depends on age" + result.get(3));
                list1.add("your Insurunce fee is " + Integer.parseInt(result.get(2)) * 0.95 * 0.95);
            } else {
                list1.add("Type : " + result.get(0));
                list1.add("Duration : " + result.get(1));
                list1.add("fee : " + result.get(2));
                list1.add("Discount rate depends on age" + result.get(3));
                list1.add("your Insurunce fee is " + Integer.parseInt(result.get(2)) * 0.95 * 0.95);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void btb_facebookActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here: 

          try {
           Server_Client_Interface rmi = (Server_Client_Interface) Naming.lookup("rmi://localhost/MyRMIserver");
           
           rmi.runExternalAPI();
           
           } catch (Exception e) {
            e.printStackTrace();
        }
        
       
        // user.facebook();
        count++;
    }

    private void btb_insurunceInformationActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        list1.clear();
        ArrayList<String> result = new ArrayList<String>();

        try {
            Server_Client_Interface rmi = (Server_Client_Interface) Naming.lookup("rmi://localhost/MyRMIserver");

            result = rmi.toString_Insurunce_information();

            list1.add(result.get(0));
            list1.add(result.get(1));
            list1.add(result.get(2));
            list1.add(result.get(3));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void btb_InsurunceJoinActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        JoinUI join = new JoinUI(getUserid());
        join.setVisible(true);
        dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CustomerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CustomerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CustomerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CustomerUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustomerUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btb_InsurunceJoin;
    private javax.swing.JButton btb_facebook;
    private javax.swing.JButton btb_insurunceInformation;
    private javax.swing.JButton btb_myinformation;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbl_Welcome;
    private java.awt.List list1;
    // End of variables declaration                   
}
